/**
 * 
 */
package com.softwareinlife.xmpp.server.servlet;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.MessageBuilder;
import com.google.appengine.api.xmpp.Presence;
import com.google.appengine.api.xmpp.PresenceType;
import com.google.appengine.api.xmpp.SendResponse;
import com.google.appengine.api.xmpp.Subscription;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;

/**
 * This XMPP bot JID is xmppsoftwareinlife@appspot.com
 * 
 * @author jangsunjin@softwareinlife.com
 * 
 */
@SuppressWarnings("serial")
public class XMPPUnavailableServlet extends HttpServlet {

	/**
	 * Logger
	 */
	public static final Logger log = Logger
			.getLogger(XMPPUnavailableServlet.class.getName());

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		log.log(Level.ALL, this.getClass().getName() + " is start!");
		
		try {
			// STEP 1
			XMPPService xmpp = XMPPServiceFactory.getXMPPService();
			Presence presence = xmpp.parsePresence(req);

			log.log(Level.INFO,
					presence.getStanza() + ", " + presence.getStatus() + ", "
							+ presence.getPresenceType() + ", "
							+ presence.getFromJid() + ", "
							+ presence.getToJid());

			xmpp.sendPresence(presence.getFromJid(), PresenceType.UNAVAILABLE,
					presence.getPresenceShow(), presence.getStatus());
		} catch (Exception e) {
			log.log(Level.SEVERE, e.getMessage());
		}
	}
}
